<?php
/**
 * AJAX Handler for Twitter: Display - Change the twitter account being displayed.
 * 
 * I have made sure Libdebug is silent in AJAX. This is because it can intefere with the HTML/Javascript as Libdebug Appends after </html>.
 * If you have a problem and need to debug comment $_LIBDEBUG->silence(); (the framework must also be in debugging mode for this to work)
 */

    $_ECLIPSEMDE = array();
    
    // Disable Templating System
    $_ECLIPSEMDE['TEMPLATING'] = false;
    require_once('../../SiteIncludes.GLOBAL.php');
    
    // Tell libdebug to never print out error information - overrides debugging flags in fwork
    $_LIBDEBUG->silence();
    
    if(EclipseMDE::runningOnMod_Rewrite() === false) {
	// Security Guard, Do Not Let This be accessed by any other means than mod_rewrite
	header("HTTP/1.1 400 Bad Request");
	die($nok_caller);
    }
    
    // Ensure all variables needed are set
    if(isset($_LOGGEDINUSER) === true && $_LOGGEDINUSER !== false && isset($gVAR['alias']) !== false && isset($gVAR['account']) !== false) {
	try {
	    $Project =& $ProjectStash->get($gVAR['alias']);
	    
	    if($Project->userCanEdit($_LOGGEDINUSER->get_userid()) === true) {
		$twitters = $Project->getTwitters();
		array_push($twitters, ($Project->getTwitterToDisplay()));
		$safe_un = $sql_conx->real_escape_string($gVAR['account']);
		$html_head = <<<EOH
			<!DOCTYPE html>
			    <html>
				<head><title></title></head>
				<body>
EOH;
		    $html_tail = <<<EOT
				</body>
			    </html>
EOT;
		if(in_array($gVAR['account'], $twitters) !== false || $gVAR['account'] == "~") {
		    // We need to unmark any for this PID that are on display
		    $query = $sql_conx->query("UPDATE `tool_twitter` SET `display`='0' WHERE `pid`='{$Project->getID()}'");
		    if($gVAR['account'] != "~") {
			$query = $sql_conx->query("UPDATE `tool_twitter` SET `display`='1' WHERE `pid`='{$Project->getID()}' AND `username`='{$safe_un}' LIMIT 1;");
		    }
		    if($sql_conx->affected_rows === 0) {
			$header = "HTTP/1.1 400 Bad Request";
			$html_body = <<<EOB
			    <script type="text/javascript">
				parent.EDJXConfirmer.show_nok();
			    </script>
EOB;
		    } else {
			$Project->updateWrite($_LOGGEDINUSER->get_userid(), ($Project->isMaintainer($_LOGGEDINUSER->get_userid()) === true ? ProjectEditType::ChangeActiveTwitter : ProjectEditType::AdminChangeActiveTwitter), array($gVAR['account'], $Project->getTwitterToDisplay()));
			$Project->updateTriggered();
			$header = "HTTP/1.1 200 OK";
			$html_body = <<<EOB
			    <script type="text/javascript">
				parent.EDJXConfirmer.show_ok();
			    </script>
EOB;
		    }
		} else {
		    $header = "HTTP/1.1 400 Bad Request";
		    $html_body = <<<EOB
			<script type="text/javascript">parent.EDJXConfirmer.show_nok();</script>
EOB;
		}
		header($header);
		die($html_head.$html_body.$html_tail);
	    } else {
		throw new InvalidProjectException();
	    }
	} catch(InvalidProjectException $e) {
	    header("HTTP/1.1 400 Bad Request");
	    die($nok_caller);
	}
    } else {
	header("HTTP/1.1 400 Bad Request");
	die($nok_caller);
    }